package net.hanjava.roas;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

/**
 * represents translated key,value pairs for specific locale
 * 
 * @author behumble@hanjava.net
 */
public class Translation {
	private Locale locale;
	private Map<String, String> map;

	/**
	 * @param locale
	 *            can not be null
	 */
	public Translation(Locale locale) {
		if (locale == null)
			throw new IllegalArgumentException("Locale can not be null");
		this.locale = locale;
		map = new HashMap<String, String>();
	}

	public Locale getLocale() {
		return locale;
	}

	public void set(String key, String value) {
		ensureKey(key);
		if (map.containsKey(key))
			throw new IllegalArgumentException("Duplicated Key : " + key);
		map.put(key, value);
	}

	public String get(String key) {
		ensureKey(key);
		String obj = map.get(key);
		return obj == null ? null : obj.toString();
	}

	public int getKeyCount() {
		return map.size();
	}

	public Iterator<String> getKeyIterator() {
		return map.keySet().iterator();
	}

	/**
	 * do key validation
	 */
	private static void ensureKey(String key) {
		if (key == null)
			throw new IllegalArgumentException("Key can not be null");
	}
}